<?php

namespace App\Repository\Service;

use App\Models\Orders\OrdersModel;
use App\Models\Service\ServiceSendMsgModel;
use App\Repository\BaseRepository;

class ServiceSendMsgRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new ServiceSendMsgModel();
//    $this->log = new Logger('anchor');
//    $this->log->pushHandler(
//      (new RotatingFileHandler(storage_path('/logs/anchor.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
//    );
        parent::__construct();
    }

    //列表
    public function index($request, $page = 1, $limit = 10)
    {
        $ModelAdminUsers = new \App\Models\Admin\AdminUsers();
        $AdminUsersTable = $ModelAdminUsers->getTable();
        $ServiceLogsTable = $this->model->getTable();
        //        查询条件
//        $where = [];
//        $where['where']['a.users_id'] = $request['user']['id'];
////        $where['where']['a.users_id'] = $request['id'];
//        $rows = [];
//        $fields = ['a.created_at', 'a.remark'];
//        $fields = array_merge($fields, ['b.nick_name']);
//        foreach ($this->model->from($ServiceLogsTable, 'a')
//                     ->leftJoin($AdminUsersTable . ' AS b', 'a.users_id', '=', 'b.id')
////                     ->leftJoin($OrdersUserTable . ' AS c', 'b.orders_user_id', '=', 'c.id')
//                     ->select($fields)
//                     ->searchWhere($where)
////                     ->page($page, $limit)
//                     ->cursor() as $row) {
//            $rows[] = $row;
//        }
////        echo $this->getLastSql();
//        return $this->returnMsg(200, '查询成功', $rows);
    }

//记录
    public function ordersLogs($ordersId)
    {
        $where = [
            'orders_id' => $ordersId
        ];
        $rows = $this->model->where($where)->orderByDesc('id')->get()->toArray();
        return $this->returnMsg(200, '查询成功', $rows);
    }

    public function add($postData)
    {
        $userId = $postData['user']['id'];
        if (empty($postData['user'])) {
            return $this->returnMsg(2, '帐号信息异常');
        }
        if (empty($postData['orders_id'])) {
            return $this->returnMsg(2, '订单信息异常');
        }
        $optionalFields = [
            'orders_id',
            'ordersno',
            'content',
            'ccid',
            'phone',
            'smsid',
            'send_phone',
            'status'
        ];
        $inData = $this->getRealData($postData, [], $optionalFields, []);
        if (empty($inData) || is_string($inData)) {
            return $this->returnMsg(
                2,
                $inData
            );
        }
        $inData['users_id'] = $userId;
        $inData['nickname'] = $postData['user']['nick_name'];
        $ordersId = $inData['orders_id'];
//        当前的订单信息
        $ModelOrder = new OrdersModel();
        $orders = $ModelOrder->where(['id' => $ordersId])->select(['status', 'service_user_id'])->first();
        $inData['service_user_id'] = $orders['service_user_id'] ?? 0;
        $inData['created_at'] = date('Y-m-d H:i:s');
        $inData['call_task_id'] = intval($postData['task_id']);
        if ($this->model->insert($inData)) {
            return $this->returnMsg(200, '短信发送信息保存成功');
        }
//        echo $this->getLastSql();
        return $this->returnMsg(2, '短信发送信息保存失败');
    }
}
